微信小程序下载图片到本地
时间 :
2022-11-06
编辑 :超级管理员
downloadImg: function(e){ //触发函数 console.log(e.currentTarget.dataset.url) wx.downloadFile({ url: e.currentTarget.dataset.url, //需要下载的图片url success: function (res) { //成功后的回调函数 wx.saveImageToPhotosAlbum({ //保存到本地 filePath: res.tempFilePath, success(res) { wx.showToast({ title: '保存成功', icon: 'success', duration: 2000 }) }, fail: function (err) { if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") { wx.openSetting({ success(settingdata) { console.log(settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) { console.log('获取权限成功,给出再次点击图片保存到相册的提示。') } else { console.log('获取权限失败,给出不给权限就无法正常使用的提示') } } }) } } }) } }); },
<button bindtap="downloadImg" data-url='https://www.tlbu.cn{{item.ico}}'>图片保存</button>